//
//  PEMDailyExerciseHeaderView.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/14.
//

import UIKit

class PEMDailyExerciseHeaderView: UIView {
    @IBOutlet var allBtn: UIButton!
    @IBOutlet var runingBtn: UIButton!
    @IBOutlet var stepLabel: UILabel!
    @IBOutlet var distanceLabel: UILabel!
    @IBOutlet var energyLabel: UILabel!
    @IBOutlet var dateLabek: UILabel!
    @IBOutlet var durationLabel: UILabel!
    
    @IBOutlet var emptyLabel: UILabel!
    @IBOutlet var otherBtn: UIButton!
    @IBOutlet var ridignBtn: UIButton!
    @IBOutlet var challengeView: UIView!
    @IBOutlet var progressView: CircleProgressView!
    var dataSource:[PEMChallengeModel] = []
    var type = -1
    var day = Date().toString(format: "yyyy.MM.dd")
    var chooseTypeBlock: ((_ type:Int,_ day:String) -> Void)?
    lazy var collectionView: UICollectionView = {
        let waitItemWidth = (Screen_Width - 10) / 2
        let waitItemSize = CGSize(width: waitItemWidth, height: 100.0)
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = waitItemSize
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.delegate = self
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .clear
        view.register(UINib(nibName: "PEMChallengeListCell", bundle: .main), forCellWithReuseIdentifier: "PEMChallengeListCell")
        view.dataSource = self
        return view
    }()
    func reloadData(_ dataSource:[PEMChallengeModel]){
        if dataSource.count > 0 {
            self.collectionView.isHidden = false
            self.emptyLabel.isHidden = true
            self.dataSource = dataSource
            self.collectionView.reloadData()
        }else{
            self.collectionView.isHidden = true
            self.emptyLabel.isHidden = false
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        clickAllAction("")
        self.dateLabek.text = Date().toString(format: "yyyy.MM.dd")
    }
    @IBAction func clickViewAllAction(_ sender: Any) {
        if let currentViewController = UIViewControllerHelper.currentViewController() {
            let vc = PEMChallengeDetailsController()
            vc.date = self.day
            currentViewController.navigationController?.pushViewController(vc, animated: true)
        }

    }
    @IBAction func clickLastDayAction(_ sender: Any) {
        self.dateLabek.text = self.dateLabek.text?.getPreviousOrNextDay(daysToAdd: -1)
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
    @IBAction func clickNextDayAction(_ sender: Any) {
        self.dateLabek.text = self.dateLabek.text?.getPreviousOrNextDay(daysToAdd: 1)
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
    @IBAction func clickSettingAction(_ sender: Any) {
        let settingView:PEMStandardSettingView = Bundle.main.loadNibNamed("PEMStandardSettingView", owner: self, options: nil)?.first as! PEMStandardSettingView
        settingView.frame = keyWindow!.frame
        keyWindow?.addSubview(settingView)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.challengeView.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    @IBAction func clickOtherAction(_ sender: Any) {
        self.otherBtn.isSelected = true
        self.allBtn.isSelected = false
        self.runingBtn.isSelected = false
        self.ridignBtn.isSelected = false
        self.type = 2
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
    @IBAction func clickRidingAction(_ sender: Any) {
        self.otherBtn.isSelected = false
        self.allBtn.isSelected = false
        self.runingBtn.isSelected = false
        self.ridignBtn.isSelected = true
        self.type = 1
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
    @IBAction func clickRuningAction(_ sender: Any) {
        self.otherBtn.isSelected = false
        self.allBtn.isSelected = false
        self.runingBtn.isSelected = true
        self.ridignBtn.isSelected = false
        self.type = 0
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
    @IBAction func clickAllAction(_ sender: Any) {
        self.otherBtn.isSelected = false
        self.allBtn.isSelected = true
        self.runingBtn.isSelected = false
        self.ridignBtn.isSelected = false
        self.type = -1
        self.chooseTypeBlock?(self.type,self.dateLabek.text!)
    }
}
extension PEMDailyExerciseHeaderView:UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:PEMChallengeListCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PEMChallengeListCell", for: indexPath) as! PEMChallengeListCell
        cell.model = self.dataSource[indexPath.item]
        return cell
    }
}
